# frozen_string_literal: true

ActiveAdmin.register_page 'DraftDecisionTable' do
  # breadcrumb do
  #   %w[admin rule_entities]
  # end
  belongs_to :rule

  # action_item :view_site do
  #   link_to "View Site", "/"
  # end

  content do
    # your content
    # form_for decision_table do |f|
    #   byebug
    #   # f.inputs do
    #     f.input :rule
    #     f.input :stage
    #     f.input :policy
    #   # end
    #
    #   f.submit
    # end
    form_for rule_entity, url: admin_rule_draftdecisiontable_create_path(rule_entity.rule_id), remote: true do |f|
      attributes_table_for rule_entity do
        row 'name' do |entity|
          link_to entity.rule.name, admin_rule_path(entity.rule)
        end

        row 'Type' do
          text_node 'RuleEntity::DecisionTable'
        end

        row :status
        row :stage
        row 'Stage' do
          f.select :policy, RuleEntity::Policy.values
        end
      end

      # f.select :policy, collection: RuleEntity::Policy.values, class: 'select2'

      if rule_entity.schema
        table_for rule_entity.schema.input_variables do
          column :name
          column 'Input Value' do |variable|
            f.input name: "inputs[#{variable.name}]"
          end
        end
      end

      hr

      div do
        f.submit 'Create a decision table'
      end
    end

    panel 'Result', class: 'fire_result' do
    end
  end

  controller do
    def index
      rule = Rule.find params.require(:rule_id)
      @rule_entity = RuleEntity.new(
        rule: rule,
        status: RuleEntity::Status::CREATED,
        stage: RuleEntity::Stage::DRAFT
      )
    end

    def create
      raise 123
    end
  end

  page_action :create, method: :post do
    raise 1111
  end

  # collection_action :draft, method: :post do
  #   # Do some CSV importing work here...
  #   redirect_to collection_path, notice: "CSV imported successfully!"
  # end
end
